package com.youyi.yyviewsdklibrary.View.shapeimageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.lkx.liandianqinew.R;

public class YYShadowImageView extends RelativeLayout {
    private boolean mInvalidat;
    private int shadowColor;
    private int shadowRound;

    public YYShadowImageView(Context context) {
        this(context, null);
    }

    public YYShadowImageView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public YYShadowImageView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.shadowRound = 0;
        this.shadowColor = -147483648;
        initView(context, attributeSet);
    }

    private void initView(Context context, AttributeSet attributeSet) {
        int i;
        setPadding(80, 40, 80, 120);
        setGravity(17);
        setLayerType(1, null);
        if (attributeSet != null) {
            TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.YYShadowImageView);
            i = obtainStyledAttributes.hasValue(R.styleable.YYShadowImageView_shadowSrc) ? obtainStyledAttributes.getResourceId(R.styleable.YYShadowImageView_shadowSrc, -1) : -1;
            this.shadowRound = obtainStyledAttributes.getDimensionPixelSize(R.styleable.YYShadowImageView_shadowRound, this.shadowRound);
            if (obtainStyledAttributes.hasValue(R.styleable.YYShadowImageView_shadowColor)) {
                this.shadowColor = obtainStyledAttributes.getColor(R.styleable.YYShadowImageView_shadowColor, Color.parseColor("#8D8D8D"));
            }
        } else {
            this.shadowRound = (int) (((float) this.shadowRound) * context.getResources().getDisplayMetrics().density);
            i = -1;
        }
        BaseRoundImageView baseRoundImageView = new BaseRoundImageView(context);
        baseRoundImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        if (i == -1) {
            baseRoundImageView.setImageResource(17170445);
        } else {
            baseRoundImageView.setImageResource(i);
        }
        if (this.shadowColor == Color.parseColor("#8D8D8D")) {
            this.shadowColor = -147483648;
        }
        addView(baseRoundImageView);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            /* class com.youyi.yyviewsdklibrary.View.shapeimageview.YYShadowImageView.AnonymousClass1 */

            public void onGlobalLayout() {
                int childCount = YYShadowImageView.this.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    View childAt = YYShadowImageView.this.getChildAt(i);
                    if (i != 0) {
                        YYShadowImageView.this.removeView(childAt);
                        YYShadowImageView.this.getChildCount();
                    } else {
                        childCount = YYShadowImageView.this.getChildCount();
                    }
                }
                ((BaseRoundImageView) YYShadowImageView.this.getChildAt(0)).setRound(YYShadowImageView.this.shadowRound);
                YYShadowImageView.this.mInvalidat = true;
            }
        });
    }

    public void setImageResource(int i) {
        ((BaseRoundImageView) getChildAt(0)).setImageResource(i);
        invalidate();
        this.mInvalidat = true;
    }

    public void setImageDrawable(Drawable drawable) {
        ((BaseRoundImageView) getChildAt(0)).setImageDrawable(drawable);
        invalidate();
        this.mInvalidat = true;
    }

    public void setImageBitmap(Bitmap bitmap) {
        ((BaseRoundImageView) getChildAt(0)).setImageBitmap(bitmap);
        invalidate();
        this.mInvalidat = true;
    }

    public void setImageShadowColor(int i) {
        this.shadowColor = i;
    }

    public void setImageRadius(int i) {
        if (i > getChildAt(0).getWidth() / 2 || i > getChildAt(0).getHeight() / 2) {
            if (getChildAt(0).getWidth() > getChildAt(0).getHeight()) {
                i = getChildAt(0).getHeight() / 2;
            } else {
                i = getChildAt(0).getWidth() / 2;
            }
        }
        this.shadowRound = i;
        ((BaseRoundImageView) getChildAt(0)).setRound(this.shadowRound);
        invalidate();
        this.mInvalidat = true;
    }

    /* access modifiers changed from: protected */
    public void dispatchDraw(Canvas canvas) {
        int i = 9;
        if (this.mInvalidat) {
            this.mInvalidat = false;
            View childAt = getChildAt(0);
            Paint paint = new Paint();
            paint.setColor(-1);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            int height = childAt.getHeight() / 12 > 40 ? 40 : childAt.getHeight() / 12;
            int i2 = 28;
            if (childAt.getHeight() / 16 <= 28) {
                i2 = childAt.getHeight() / 16;
            }
            ImageView imageView = (ImageView) childAt;
            if (imageView.getDrawable() instanceof ColorDrawable) {
                paint.setShadowLayer(40.0f, 0.0f, 28.0f, getDarkerColor(((ColorDrawable) imageView.getDrawable()).getColor()));
            } else if (imageView.getDrawable() instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//                Palette.Swatch dominantSwatch = Palette.from(bitmap).generate().getDominantSwatch();
//                if (dominantSwatch != null) {
//                    i = dominantSwatch.getRgb();
//                } else {
//                    i = Color.parseColor("#8D8D8D");
//                }
                float f = (float) height;
                float f2 = (float) i2;
                paint.setShadowLayer(f, 0.0f, f2, getDarkerColor(i));
                Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, (bitmap.getHeight() / 4) * 3, bitmap.getWidth(), bitmap.getHeight() / 4);
//                if (Palette.from(createBitmap).generate().getDominantSwatch() != null) {
//                    paint.setShadowLayer(f, 0.0f, f2, Palette.from(createBitmap).generate().getDominantSwatch().getRgb());
//                }
            } else {
                paint.setShadowLayer((float) height, 0.0f, (float) i2, getDarkerColor(Color.parseColor("#8D8D8D")));
            }
            int i3 = this.shadowColor;
            if (i3 != -147483648) {
                paint.setShadowLayer((float) height, 0.0f, (float) i2, i3);
            }
            RectF rectF = new RectF(childAt.getX() + ((float) (childAt.getWidth() / 20)), childAt.getY(), (childAt.getX() + ((float) childAt.getWidth())) - ((float) (childAt.getWidth() / 20)), (childAt.getY() + ((float) childAt.getHeight())) - ((float) (childAt.getHeight() / 40)));
            int i4 = this.shadowRound;
            canvas.drawRoundRect(rectF, (float) i4, (float) i4, paint);
            canvas.save();
        }
        super.dispatchDraw(canvas);
    }

    public int getDarkerColor(int i) {
        float[] fArr = new float[3];
        Color.colorToHSV(i, fArr);
        fArr[1] = fArr[1] + 0.1f;
        fArr[2] = fArr[2] - 0.1f;
        return Color.HSVToColor(fArr);
    }
}
